package edu.nf.api.controller.rider;

import edu.nf.api.controller.BaseController;
import edu.nf.util.JwtUtil;
import edu.nf.util.sms.SMSUtil;
import edu.nf.waimai.model.rider.Rider;
import edu.nf.waimai.service.rider.impl.RiderServiceImpl;
import jdk.nashorn.internal.codegen.DumpBytecode;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.JsonView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;


/**
 * @Date 2023-06-21
 * @Author qiu
 * 骑手登录
 */
public class LoginRiderController extends BaseController {

    // 判断输入的验证码和生成的验证码是否一致
    String send = null;

    @RequestMapping("/rider/send")
    public View sendKey(@RequestParam("Phone") String riderPhone){
        send = new SMSUtil().send(riderPhone);
        // 保存到会话作用域中
        return new JsonView(success(send));
    }

    /**
     * @param
     * @param riderPhone 骑手手机号
     * @return
     */
    @RequestMapping("/rider/login")
    public View riderLogin(@RequestParam("riderPhone") String riderPhone,@RequestParam("key") String key,HttpSession session ) {
        System.out.println(key + riderPhone + "asdasdasd");

        Rider rider = new RiderServiceImpl().loginRider(key, riderPhone);

        // 获取登录信息，存储到 token 中
        HashMap<String, Object> map = new HashMap<>();
        map.put("rider_phone", rider.getRiderPhone());

        // 创建 token
        String token = JwtUtil.createToken(map, 5 * 60 * 1000);

//        // 把保存的验证码从会话中取出
//        String code = (String) session.getAttribute("code");

        return new JsonView(success());
    }
}
